﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace NewsWcfWebService
{
    public class ResourceLoader
    {
        static ResourceLoader instance;
        XmlDocument xDoc;

        static string _language;

        public static string Language
        {
            get { return ResourceLoader._language; }
            set { ResourceLoader._language = value; }
        }

     
        public XmlDocument XDoc
        {
            get { return xDoc; }
            set { xDoc = value; }
        }
        static XmlNodeList listNode;

        public static XmlNodeList ListNode
        {
            get { return ResourceLoader.listNode; }
            set { ResourceLoader.listNode = value; }
        }

        
        public static ResourceLoader getInstance()
        {
            if (instance == null)
            {
                instance = new ResourceLoader();
            }

            return instance;
        }

        public ResourceLoader()
        {
            //string sPathName = "././KEY_DEFINE_NAME.xml";
            string sPathId = "././KEY_DEFINE_ID.xml";
            xDoc = new XmlDocument();
            xDoc.LoadXml(sPathId);
            listNode = xDoc.SelectNodes(".//KEY");
        }
        public  string getValueNode( XmlNode node)
        {
            // key vi=Viet Nam; en=English 
            XmlNode chidNode = node.SelectSingleNode(".//VALUE[@lag="+ Language+"]");
            if (chidNode.Value.ToString() != null)
                return chidNode.Value.ToString();
            return "Key Empty";
        }
        public  string stringWithKey(string key)
        {
            foreach (XmlNode node in listNode)
            {
                if (node.Value.CompareTo(key) == 0)
                {
                    return getValueNode(node);
                }
            }
            return "";
        }
        public  string getIdOfKeyName(string keyName)
        {
            string strResult = null;
            foreach (XmlNode node in listNode)
            {
                XmlNodeList list = node.ChildNodes;
                foreach (XmlNode child in list)
                {
                    if (child.Value.ToString().CompareTo(keyName) == 0)
                    {
                        strResult = node.Attributes["id"].Value.ToString();
                    }
                }
            }
            return strResult;
        }
    }
}
